package com.chaosj.algorithm

import com.chaosj.algorithm.SwapNode.ListNode

class ReverseNode {
    static void main(args){
        def node1 = new ListNode(1)
        def node2 = new ListNode(2)
        def node3 = new ListNode(3)
        def node4 = new ListNode(4)
        def node5 = new ListNode(5)
        node1.next = node2
        node2.next = node3
        node3.next = node4
        node4.next = node5
        println(solution(node1,2))
        println(solution(node1,3))
    }

    static ListNode solution(ListNode node,int deep){
        Stack stack = new Stack()
        while (stack.size()<deep){
            ListNode tmp = node
            tmp.next = null
            stack.add(tmp)
            node = node.next
        }
        ListNode result = stack.pop()
        while(!stack.isEmpty()){
            ListNode tmp = stack.pop()
            result.next = tmp
            tmp = result.next
        }
    }
}
